package com.hsytech.dijiebao.expand.voice;

import com.iflytek.cloud.SpeakerVerifier;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechListener;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;

public class BaseVoice {
	private SpeechSynthesizer mTts;
	public static BaseVoice baseVoice;
	private SpeakerVerifier speakerVerifier;

	private BaseVoice(Context context) {
		mTts = SpeechSynthesizer.createSynthesizer(context, null);
		// ����SpeakerVerifier����
		speakerVerifier = SpeakerVerifier.createVerifier(context, null);
	}

	public static BaseVoice getInstance(Context context) {

		if (baseVoice == null) {
			baseVoice = new BaseVoice(context);
		}

		return baseVoice;
	}

	public void setVoice(String sayPerson) {
		// 1.����SpeechSynthesizer����, �ڶ������������غϳ�ʱ��InitListener
		// 2.�ϳɲ������ã�������ƴ�Ѷ��MSC API�ֲ�(Android)��SpeechSynthesizer ��
		mTts.setParameter(SpeechConstant.VOICE_NAME, sayPerson);// ���÷�����
		mTts.setParameter(SpeechConstant.SPEED, "50");// ��������
		mTts.setParameter(SpeechConstant.VOLUME, "100");// ������������Χ0~100
		mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD); // �����ƶ�
		// ���úϳ���Ƶ����λ�ã����Զ��屣��λ�ã��������ڡ�./sdcard/iflytek.pcm��
		// ������SD����Ҫ��AndroidManifest.xml���дSD��Ȩ��
		// �������Ҫ����ϳ���Ƶ��ע�͸��д���
		// mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH,
		// "./sdcard/iflytek.pcm");
		// 3.��ʼ�ϳ�
		// mTts.startSpeaking("�µ��ó�,���ǰ���,�ؽӱ�", mSynListener);

	}

	public void startSpeaking(String message) {
		if (message.equals("")) {
			mTts.startSpeaking("The new journey, we accompany, to meet the treasure,finally we are family",
					mSynListener);

		} else {

			mTts.startSpeaking(message, mSynListener);
		}

	}

	public void signIn() {
		// ͨ��setParameter�����������ͣ�pwdType��ȡֵΪ1��2��3���ֱ��ʾ�ı����롢����˵����������
		speakerVerifier.setParameter(SpeechConstant.ISV_PWDT, "" + "1");
		SpeechListener mPwdListenter = new SpeechListener() {
			public void onEvent(int eventType, Bundle params) {
			}

			public void onBufferReceived(byte[] buffer) {

			}

			public void onCompleted(SpeechError error) {
			}
		};
		speakerVerifier.getPasswordList(mPwdListenter);
	}

	// �ϳɼ�����
	private SynthesizerListener mSynListener = new SynthesizerListener() {
		// �Ự�����ص��ӿڣ�û�д���ʱ��errorΪnull
		public void onCompleted(SpeechError error) {
			Log.d("mylog", "voice  error:" + error);
		}

		// ������Ȼص�
		// percentΪ�������0~100��beginPosΪ������Ƶ���ı��п�ʼλ�ã�endPos��ʾ������Ƶ���ı��н���λ�ã�infoΪ������Ϣ��
		public void onBufferProgress(int percent, int beginPos, int endPos, String info) {
		}

		// ��ʼ����
		public void onSpeakBegin() {
		}

		// ��ͣ����
		public void onSpeakPaused() {
		}

		// ���Ž��Ȼص�
		// percentΪ���Ž���0~100,beginPosΪ������Ƶ���ı��п�ʼλ�ã�endPos��ʾ������Ƶ���ı��н���λ��.
		public void onSpeakProgress(int percent, int beginPos, int endPos) {
		}

		// �ָ����Żص��ӿ�
		public void onSpeakResumed() {
		}

		// �Ự�¼��ص��ӿ�
		public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
		}
	};
}
